package com.andromouse;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;

public class CommandHandle {
	String clientSentence = null;
	SystemOperations SO = new SystemOperations();
	MediaController MC = new MediaController();
	public static int scroll;
	public static Robot ambot;

	public CommandHandle(String myCommand) {
		this.clientSentence = myCommand;
		try {
			ambot = new Robot();
		} catch (AWTException e) {
			System.out.println("Error Creating AndroMouse Robot");
		}
	}

	public void handle() throws Exception {
		int xint = 0;
		int yint = 0;
		boolean mySkip = true;

		int myCount = 0;

		PointerInfo a = MouseInfo.getPointerInfo();
		Point b = a.getLocation();
		int x = (int) b.getX();
		int y = (int) b.getY();

		if (this.clientSentence.contains("click")) {
			ClickMouse();
		} else if (this.clientSentence.contains("AMLogoff")) {
			this.SO.doSystemOperation(1);
		} else if (this.clientSentence.contains("AMLock")) {
			this.SO.doSystemOperation(2);
		} else if (this.clientSentence.contains("AMRestart")) {
			this.SO.doSystemOperation(3);
		} else if (this.clientSentence.contains("AMSleep")) {
			this.SO.doSystemOperation(4);
		} else if (this.clientSentence.contains("AMShutdown")) {
			this.SO.doSystemOperation(5);
		} else if (this.clientSentence.contains("EscKey")) {
			doType(27);
		} else if (this.clientSentence.contains("CapsKey")) {
			doType(20);
		} else if (this.clientSentence.contains("ScrollKey")) {
			doType(145);
		} else if (this.clientSentence.contains("NumKey")) {
			doType(144);
		} else if (this.clientSentence.contains("PrintKey")) {
			doType(154);
		} else if (this.clientSentence.contains("F1Key")) {
			doType(112);
		} else if (this.clientSentence.contains("F2Key")) {
			doType(113);
		} else if (this.clientSentence.contains("F3Key")) {
			doType(114);
		} else if (this.clientSentence.contains("F4Key")) {
			doType(115);
		} else if (this.clientSentence.contains("F5Key")) {
			doType(116);
		} else if (this.clientSentence.contains("F6Key")) {
			doType(117);
		} else if (this.clientSentence.contains("F7Key")) {
			doType(118);
		} else if (this.clientSentence.contains("F8Key")) {
			doType(119);
		} else if (this.clientSentence.contains("F9Key")) {
			doType(120);
		} else if (this.clientSentence.contains("F10Key")) {
			doType(121);
		} else if (this.clientSentence.contains("F11Key")) {
			doType(122);
		} else if (this.clientSentence.contains("F12Key")) {
			doType(123);
		} else if (this.clientSentence.contains("CtrlKey")) {
			doType(17);
		} else if (this.clientSentence.contains("UpKey")) {
			doType(38);
		} else if (this.clientSentence.contains("AltKey")) {
			doType(18);
		} else if (this.clientSentence.contains("LeftKey")) {
			doType(37);
		} else if (this.clientSentence.contains("DownKey")) {
			doType(40);
		} else if (this.clientSentence.contains("RightKey")) {
			doType(39);
		} else if (this.clientSentence.contains("InsertKey")) {
			doType(155);
		} else if (this.clientSentence.contains("HomeKey")) {
			doType(36);
		} else if (this.clientSentence.contains("PgupKey")) {
			doType(33);
		} else if (this.clientSentence.contains("PgdownKey")) {
			doType(34);
		} else if (this.clientSentence.contains("EndKey")) {
			doType(35);
		} else if (this.clientSentence.contains("ShiftKey")) {
			System.out.println("regular shift");
			doType(16);
		} else if (this.clientSentence.contains("DelKey")) {
			doType(127);
		} else if (this.clientSentence.contains("TabKey")) {
			doType(9);
			System.out.println("tab");
		} else if (this.clientSentence.contains("EnterKey")) {
			doType(10);
		} else if (this.clientSentence.contains("mpBack")) {
			this.MC.doMediaControl(1);
		} else if (this.clientSentence.contains("mpPlay")) {
			this.MC.doMediaControl(0);
		} else if (this.clientSentence.contains("mpNext")) {
			this.MC.doMediaControl(2);
		} else if (this.clientSentence.contains("mpStop")) {
			this.MC.doMediaControl(3);
		} else if (this.clientSentence.contains("mpMute")) {
			this.MC.doMediaControl(4);
		} else if (this.clientSentence.contains("mpVolDown")) {
			this.MC.doMediaControl(6);
		} else if (this.clientSentence.contains("mpVolUp")) {
			this.MC.doMediaControl(5);
		} else if (this.clientSentence.contains("mpSuffle")) {
			this.MC.doMediaControl(7);
		} else if (this.clientSentence.contains("mpRepeat")) {
			this.MC.doMediaControl(8);
		} else if (this.clientSentence.contains("mpFullScreen")) {
			this.MC.doMediaControl(9);
		} else if (this.clientSentence.contains("mpClose")) {
			this.MC.doMediaControl(11);
		} else if (this.clientSentence.contains("CtrlKDwn")) {
			System.out.println("CTRL DOWN");
			doTypeDown(17);
			countDown(1);
		} else if (this.clientSentence.contains("AltKDwn")) {
			System.out.println("Now alt down");
			doTypeDown(18);
			countDown(2);
		} else if (this.clientSentence.contains("ShiftKDwn")) {
			doTypeDown(16);
			countDown(3);
		} else if (this.clientSentence.contains("CtrlKU")) {
			releaseKeys(1);
		} else if (this.clientSentence.contains("AltKU")) {
			releaseKeys(2);
		} else if (this.clientSentence.contains("ShiftKU")) {
			releaseKeys(3);
		} else if (this.clientSentence.contains("lngclk")) {
			LngClkDwn();
		} else if (this.clientSentence.contains("lngclu")) {
			LngClkUp();
		} else if (this.clientSentence.contains("rtclk")) {
			RtClick();
		} else if (this.clientSentence != null) {
			if (this.clientSentence.contains("DRAG")) {
				System.out.println("DRAG START");
			} else if (this.clientSentence.contains("scrl")) {
				if (this.clientSentence.contains("scrldwn"))
					scroll = 1;
				else if (this.clientSentence.contains("scrlup")) {
					scroll = -1;
				} else if (this.clientSentence.contains("scrlLeft")) {
					scroll = 2;
				} else if (this.clientSentence.contains("scrlRight")) {
					scroll = 3;
				}

				Scroll(scroll);
			} else if (this.clientSentence.contains("myDrgOn")) {
				LngClkDwn();
			} else if (this.clientSentence.contains("myDrgOff")) {
				LngClkUp();
			} else if (this.clientSentence.contains("bkspc")) {
				type(999);
			} else if (this.clientSentence.contains(":SpechRec")) {
				String mySpeechTemp = this.clientSentence.substring(0,
						this.clientSentence.indexOf(":"));
				System.out.println(mySpeechTemp);
				typeSpeech(mySpeechTemp);
			} else if (!this.clientSentence.contains("ANDROMOUSE")) {
				if (!this.clientSentence.contains("AMSNIFF")) {
					if (!this.clientSentence.contains("AmEndShow")) {
						if (this.clientSentence.contains("MVEX")) {
							int newx = xint;
							int newy = yint;
							String xval = this.clientSentence.substring(0,
									this.clientSentence.indexOf("x"));
							this.clientSentence = this.clientSentence
									.substring(this.clientSentence.indexOf("x") + 1);
							String yval = this.clientSentence.substring(0,
									this.clientSentence.indexOf("y"));

							xint = Integer.parseInt(xval);
							yint = Integer.parseInt(yval);
							int mydiffx = xint - newx;
							int mydiffy = yint - newy;
							int diffX = x + mydiffx;
							int diffY = y + mydiffy;
							if (mySkip) {
								MouseMove(diffX, diffY);
							}

							if (myCount > 1) {
								mySkip = true;
							}
							myCount++;
						} else if (this.clientSentence.contains("skpp")) {
							myCount = 0;
							mySkip = false;
						} else {
							type(this.clientSentence);
						}
					}
				}
			}
		}
		this.clientSentence = "";
	}

	public static void MouseMove(int x, int y) {
		ambot.setAutoDelay(5);
		ambot.mouseMove(x, y);
	}

	public void ClickMouse() {
		ambot.mousePress(16);
		ambot.mouseRelease(16);
	}

	public static void RtClick() throws Exception {
		ambot.mousePress(4);
		ambot.mouseRelease(4);
	}

	public static void type(String m) throws Exception {
		byte[] bytes = m.getBytes();
		byte[] arrayOfByte1;
		if ((arrayOfByte1 = bytes).length != 0) {
			byte b = arrayOfByte1[0];
			int code = b;

			if (code > 0)
				type(code);
		}
	}

	public static void Scroll(int scroll) throws Exception {
		boolean isWin = false;
		String operatingSystem = System.getProperty("os.name");
		if (operatingSystem.contains("Win")) {
			isWin = true;
		}

		switch (scroll) {
		case 1:
			ambot.mouseWheel(scroll);
			break;
		case -1:
			ambot.mouseWheel(scroll);
			break;
		case 2:
			if (isWin) {
				ambot.keyPress(18);
				ambot.keyPress(9);
				ambot.keyRelease(9);
				ambot.keyPress(37);
				ambot.keyRelease(37);
				ambot.keyRelease(18);
			} else {
				ambot.keyPress(157);
				ambot.keyPress(9);
				ambot.keyRelease(9);
				ambot.keyPress(37);
				ambot.keyRelease(37);
				ambot.keyRelease(157);
			}
			break;
		case 3:
			if (isWin) {
				ambot.keyPress(18);
				ambot.keyPress(9);
				ambot.keyRelease(9);
				ambot.keyPress(39);
				ambot.keyRelease(39);
				ambot.keyRelease(18);
			} else {
				ambot.keyPress(157);
				ambot.keyPress(9);
				ambot.keyRelease(9);
				ambot.keyPress(39);
				ambot.keyRelease(39);
				ambot.keyRelease(157);
			}
		case 0:
		}
	}

	public static void LngClkDwn() throws Exception {
		ambot.mousePress(16);
	}

	public static void LngClkUp() throws Exception {
		ambot.mouseRelease(16);
	}

	public static void type(int keycode) throws AWTException {
		switch (keycode) {
		case 97:
			doType(65);
			break;
		case 98:
			doType(66);
			break;
		case 99:
			doType(67);
			break;
		case 100:
			doType(68);
			break;
		case 101:
			doType(69);
			break;
		case 102:
			doType(70);
			break;
		case 103:
			doType(71);
			break;
		case 104:
			doType(72);
			break;
		case 105:
			doType(73);
			break;
		case 106:
			doType(74);
			break;
		case 107:
			doType(75);
			break;
		case 108:
			doType(76);
			break;
		case 109:
			doType(77);
			break;
		case 110:
			doType(78);
			break;
		case 111:
			doType(79);
			break;
		case 112:
			doType(80);
			break;
		case 113:
			doType(81);
			break;
		case 114:
			doType(82);
			break;
		case 115:
			doType(83);
			break;
		case 116:
			doType(84);
			break;
		case 117:
			doType(85);
			break;
		case 118:
			doType(86);
			break;
		case 119:
			doType(87);
			break;
		case 120:
			doType(88);
			break;
		case 121:
			doType(89);
			break;
		case 122:
			doType(90);
			break;
		case 65:
			doType(1, 65);
			break;
		case 66:
			doType(1, 66);
			break;
		case 67:
			doType(1, 67);
			break;
		case 68:
			doType(1, 68);
			break;
		case 69:
			doType(1, 69);
			break;
		case 70:
			doType(1, 70);
			break;
		case 71:
			doType(1, 71);
			break;
		case 72:
			doType(1, 72);
			break;
		case 73:
			doType(1, 73);
			break;
		case 74:
			doType(1, 74);
			break;
		case 75:
			doType(1, 75);
			break;
		case 76:
			doType(1, 76);
			break;
		case 77:
			doType(1, 77);
			break;
		case 78:
			doType(1, 78);
			break;
		case 79:
			doType(1, 79);
			break;
		case 80:
			doType(1, 80);
			break;
		case 81:
			doType(1, 81);
			break;
		case 82:
			doType(1, 82);
			break;
		case 83:
			doType(1, 83);
			break;
		case 84:
			doType(1, 84);
			break;
		case 85:
			doType(1, 85);
			break;
		case 86:
			doType(1, 86);
			break;
		case 87:
			doType(1, 87);
			break;
		case 88:
			doType(1, 88);
			break;
		case 89:
			doType(1, 89);
			break;
		case 90:
			doType(1, 90);
			break;
		case 96:
			doType(192);
			break;
		case 48:
			doType(48);
			break;
		case 49:
			doType(49);
			break;
		case 50:
			doType(50);
			break;
		case 51:
			doType(51);
			break;
		case 52:
			doType(52);
			break;
		case 53:
			doType(53);
			break;
		case 54:
			doType(54);
			break;
		case 55:
			doType(55);
			break;
		case 56:
			doType(56);
			break;
		case 57:
			doType(57);
			break;
		case 45:
			doType(45);
			break;
		case 61:
			doType(61);
			break;
		case 126:
			doType(1, 192);
			break;
		case 33:
			doType(1, 49);
			break;
		case 64:
			doType(1, 50);
			break;
		case 35:
			doType(1, 51);
			break;
		case 36:
			doType(1, 52);
			break;
		case 37:
			doType(1, 53);
			break;
		case 94:
			doType(1, 54);
			break;
		case 38:
			doType(1, 55);
			break;
		case 42:
			doType(1, 56);
			break;
		case 40:
			doType(1, 57);
			break;
		case 41:
			doType(1, 48);
			break;
		case 95:
			doType(1, 45);
			break;
		case 43:
			doType(1, 61);
			break;
		case 9:
			doType(9);
			break;
		case 10:
			doType(10);
			break;
		case 91:
			doType(91);
			break;
		case 93:
			doType(93);
			break;
		case 92:
			doType(92);
			break;
		case 123:
			doType(1, 91);
			break;
		case 125:
			doType(1, 93);
			break;
		case 124:
			doType(1, 92);
			break;
		case 59:
			doType(59);
			break;
		case 39:
			doType(222);
			break;
		case 34:
			doType(1, 222);
			break;
		case 44:
			doType(44);
			break;
		case 60:
			doType(1, 44);
			break;
		case 46:
			doType(46);
			break;
		case 62:
			doType(1, 46);
			break;
		case 47:
			doType(47);
			break;
		case 63:
			doType(1, 47);
			break;
		case 32:
			doType(32);
			break;
		case 999:
			doType(8);
		}
	}

	private static void doType(int keyCodes) throws AWTException {
		ambot.keyPress(keyCodes);
		ambot.keyRelease(keyCodes);
	}

	private static void doType(int a, int keyCodes) throws AWTException {
		ambot.keyPress(16);
		ambot.keyPress(keyCodes);
		ambot.keyRelease(keyCodes);
		ambot.keyRelease(16);
	}

	private static void doTypeDown(int keyCodes) throws AWTException {
		ambot.keyPress(keyCodes);
	}

	public static void typeSpeech(String m) throws Exception {
		byte[] bytes = m.getBytes();
		for (byte b : bytes) {
			int code = b;
			System.out.println(b);
			if (code > 0)
				type(code);
		}
	}

	public void countDown(int myCodes) {
		/*new Thread(myCodes) {
			public void run() {
				try {
			          sleep(10000L);
			          CommandHandle.this.releaseKeys(this.val$myCodes); } catch (InterruptedException localInterruptedException) {
			        }
			}
		}.start();*/
	}

	public void releaseKeys(int codes) {
		switch (codes) {
		case 1:
			ambot.keyRelease(17);

			break;
		case 2:
			ambot.keyRelease(18);

			break;
		case 3:
			ambot.keyRelease(16);
		}
	}
}